/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ddbserver.common;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 *
 * @author Roar
 */
public class Table {

    private String tablename;
    private HashMap<String, String> cols;
    private String key;

    public Table(String tablename) {
        this.tablename = tablename;
        this.cols = new LinkedHashMap<String, String>();
        this.key = new String();
    }

    public void insertCols(String colname, String coltype) {
        cols.put(colname, coltype);
    }

    public String getKey() {
        return this.key;
    }
    
    public void setKey(String key){
        this.key = key;
    }
    
    public String getType(String colname){
        return cols.get(colname);
    }

    public Set<String>getCols(){
        return cols.keySet();
    }

    public int getSize(){
        return cols.size();
    }

    /*
     generate a sql for create table by the colsNames, for example: colsNames = "id", "name"
     then the return sql is "(id int primary key, name char(2))"
     */
    public String generateCreateTableSQL(String[] colsNames, String tableName) {
        String sql = new String();
        for(int i = 0; i < colsNames.length; i++) {
            for(String col : cols.keySet()) {
                if(colsNames[i].equalsIgnoreCase(col)) {
                    sql = sql + colsNames[i] + " " + this.getType(colsNames[i]);
                    if(getKey().equalsIgnoreCase(colsNames[i]) || col.equals("id")){
                        sql = sql + " primary key";
                    }
                    if(i != colsNames.length - 1) {
                        sql = sql + ",";
                    }
                }
            }
        }
        sql = "create table " + tableName + "(" + sql + ")";
        return sql;
    }

    public String generateCreateTableSQL(String tableName) {
        String sql = new String();
        int count = 0;
        for(String col : cols.keySet()) {
            sql = sql + col + " " + this.getType(col);
            if(col.equals(key) || col.equals("id")) {
                sql = sql + " primary key";
            }
            count++;
            if(count != this.getSize()) {
                sql = sql + ",";
            }
        }
        sql = "create table "  + tableName + "(" + sql + ")";
        return sql;
    }

    public String generateImportDataSQL(String file) {
        String sql = new String();
        String columns = new String();
        int count = 0;
        for(String col : cols.keySet()) {
            sql = sql + col;
            count++;
            if(count != this.getSize()) {
                sql = sql + ",";
            }
        }
        String tableName = "temp_" + this.tablename;
        sql = "LOAD DATA LOCAL INFILE \'" + file.replace("\\", "/") + "\' INTO TABLE " + tableName
                + " FIELDS TERMINATED BY \',\' ENCLOSED BY \'" + "\\'" + "\' (" + columns + ")";
        return sql;
    }

}
